/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddStudentFrame.java
 *
 * Created on 2010-6-12, 16:45:53
 */

package demo1029;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;

/**
 *
 * @author ls
 */
public class AddStudentFrame extends javax.swing.JFrame {
   BanjiDAO banjiDAO=new BanjiDAO();
    /** Creates new form AddStudentFrame */
    public AddStudentFrame() {
        initComponents();
        //Vector model = banjiDAO.getBanjiData(); 
    //ArrayList
    //mvc

//    model.addElement(new Item(1, "1班"));
//    model.addElement(new Item(2, "2班"));
//    model.addElement(new Item(3, "3班"));
//    model.addElement(new Item(4, "4班"));
        //String banji[]={"计算机","电子"};
        //Banji bjs[]={ new Banji("01","计算机") ,new Banji("02","电子")};
        Banji bjs[]=banjiDAO.findAll();
        
    DefaultComboBoxModel dcbm=new DefaultComboBoxModel(bjs);
    
    this.jComboBox1.setModel(dcbm);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        no = new javax.swing.JTextField();
        name = new javax.swing.JTextField();
        math = new javax.swing.JTextField();
        english = new javax.swing.JTextField();
        physics = new javax.swing.JTextField();
        add = new javax.swing.JButton();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
        jLabel1.setText("学号");

        jLabel2.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
        jLabel2.setText("姓名");

        jLabel3.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
        jLabel3.setText("数学成绩");

        jLabel4.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
        jLabel4.setText("英语成绩");

        jLabel5.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
        jLabel5.setText("物理成绩");

        no.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N

        name.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N

        math.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N

        english.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N

        physics.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N

        add.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
        add.setText("添加学生");
        add.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addActionPerformed(evt);
            }
        });

        jLabel6.setText("班级");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(68, 68, 68)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6))
                        .addGap(46, 46, 46)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(no, javax.swing.GroupLayout.DEFAULT_SIZE, 97, Short.MAX_VALUE)
                            .addComponent(math)
                            .addComponent(english)
                            .addComponent(physics)
                            .addComponent(name)
                            .addComponent(jComboBox1, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(110, 110, 110)
                        .addComponent(add)))
                .addContainerGap(162, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(17, 17, 17)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(no, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(math, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(english, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(physics, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(add)
                .addContainerGap(65, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void addActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addActionPerformed
        // TODO add your handling code here:
        String no1=this.no.getText();
        String name1=this.name.getText();
        int math1=Integer.parseInt(this.math.getText());
        int english1=Integer.parseInt(this.english.getText());
        int physics1=Integer.parseInt(this.physics.getText());
        Banji item=(Banji)jComboBox1.getSelectedItem();
        String classno=item.getNo();
        Connection conn = null;
        PreparedStatement pstat = null;//语句

        try {
            Class.forName("com.mysql.jdbc.Driver");//生成Driver对象
            String url = "jdbc:mysql://localhost:3306/st";
            conn = DriverManager.getConnection(url, "root", "123");
            String insertStr = "insert into " +
                    "students values(?,?,?,?,?,?)";
            //pstat表示inserStr里的insert into
            pstat = conn.prepareStatement(insertStr);//建立Sql命令对象
            pstat.setString(1, no1);
            pstat.setString(2, name1);
            pstat.setInt(3, math1);
            pstat.setInt(4, english1);
            pstat.setInt(5, physics1);
            pstat.setString(6,classno);
            
            int i = pstat.executeUpdate();
           // System.out.println(i);
            if (i > 0) {
              JOptionPane.showMessageDialog(this, "添加成功");
            } else {
                 JOptionPane.showMessageDialog(this, "添加失败");
            }
        } catch (ClassNotFoundException e) {
            System.out.println(e.getMessage());
            System.out.println("驱动错误");
        } catch (SQLException e1) {
            System.out.println(e1.getMessage());
            System.out.println("数据库错误");
        }
    }//GEN-LAST:event_addActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new AddStudentFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton add;
    private javax.swing.JTextField english;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JTextField math;
    private javax.swing.JTextField name;
    private javax.swing.JTextField no;
    private javax.swing.JTextField physics;
    // End of variables declaration//GEN-END:variables

}
